package com.iddactylogram.common.domain;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.imps.CuratorFrameworkImpl;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class IdDactylogramNamespace extends CuratorFrameworkImpl {

    private final static String DEFAULT_NAMESPACE_ID_DACTYLOGRAM = "iddactylogram";


    public IdDactylogramNamespace(CuratorFramework parent) {
        super((CuratorFrameworkImpl) parent);
        initNamespace();
    }

    public IdDactylogramNamespace(CuratorFrameworkFactory.Builder builder) {
        super(builder);
        initNamespace();
    }

    public void initNamespace() {
        try {
            Stat stat = this.checkExists().forPath("/" + DEFAULT_NAMESPACE_ID_DACTYLOGRAM);
            if (stat == null) {
                this.create().withMode(CreateMode.PERSISTENT).forPath("/" + DEFAULT_NAMESPACE_ID_DACTYLOGRAM);
            }
            this.usingNamespace(DEFAULT_NAMESPACE_ID_DACTYLOGRAM);
        } catch (Exception e) {
            throw new IllegalArgumentException("IdDactylogramNamespace初始化失败", e);
        }
    }
}
